package com.neolix.watch;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;

import com.google.gson.reflect.TypeToken;
import com.neolix.watch.bluetooth.BlueToothUtil;
import com.neolix.watch.dao.RemindEvent;
import com.neolix.watch.domain.RemindEventViewModel;
import com.neolix.watch.util.Constant;
import com.neolix.watch.util.RunTimeDatabaseHelper;
import com.neolix.watch.util.StringUtil;
import com.neolix.watch.view.CustomDateTimePickerView;

import org.joda.time.DateTime;

import static com.neolix.watch.NeolixApplication.getInstance;
import static com.neolix.watch.util.JsonConverter.convertJsonToObject;
import static com.neolix.watch.util.StringUtil.showText;

public class AddRemindActivity extends NeolixNaviagationBaseActivity {

    private EditText contentEditText;
    private RemindEventViewModel model;
    private RunTimeDatabaseHelper databaseHelper;
    private CustomDateTimePickerView dateTimePickerView;
    private int newEventID;

    public AddRemindActivity() {
        super(R.layout.activity_add_remind);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getIntent().getSerializableExtra(Constant.REMIND_EVENT_VIEW_MODEL) != null) {
            String jsonString = getIntent().getStringExtra(Constant.REMIND_EVENT_VIEW_MODEL);
            model = convertJsonToObject(jsonString, TypeToken.get(RemindEventViewModel.class));
        }

        newEventID = getIntent().getIntExtra(Constant.EVENT_ID, -1);
        databaseHelper = RunTimeDatabaseHelper.initial(getApplicationContext());

        initUi();
        initDisplayData();
    }

    private void initDisplayData() {
        DateTime time;
        time = ((model == null) ? DateTime.now() : new DateTime(model.getDateTime()));

        contentEditText.setText(model == null ? "" : model.getContent());
        dateTimePickerView.setDateTime(time);
    }

    private void initUi() {
        setTitle(model == null ? getString(R.string.add_event) : getString(R.string.edit_event));
        dateTimePickerView = (CustomDateTimePickerView) findViewById(R.id.customer_date_time_picker);
        contentEditText = (EditText) findViewById(R.id.content_edit_text);

        setLeftButton(R.drawable.selector_no);
        setRightButton(R.drawable.selector_ok);

        setLeftButtonClickedListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

        setRightButtonClickedListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (model == null)
                    addRemindEvent();
                else
                    updateRemindEvent();
            }
        });
    }

    private void updateRemindEvent() {
        String contentString = contentEditText.getText().toString();
        if (StringUtil.isNotBlank(contentString)) {
            DateTime eventTime = dateTimePickerView.getDateTime();
            if (eventTime.isBeforeNow()) {
                showText(getString(R.string.time_must_greate_than_now));
                return;
            }

            model.setContent(contentString);
            model.setDateTime(eventTime.toDate());

            RemindEvent event = model.getRemindEvent();
            if (getInstance().isConnectWatch()) {
                BlueToothUtil.syncRemindEvent(2, event);
                databaseHelper.updateRemindEvent(event);

                setResult(RESULT_OK);
                finish();
            } else {
                showText(getString(R.string.update_remind_event_failure));
            }
        }
    }

    private void addRemindEvent() {
        String contentString = contentEditText.getText().toString();
        if (StringUtil.isNotBlank(contentString)) {
            DateTime eventTime = dateTimePickerView.getDateTime();
            if (eventTime.isBeforeNow()) {
                showText(getString(R.string.time_must_greate_than_now));
                return;
            }

            RemindEvent event = new RemindEvent();
            event.setEventID(newEventID);
            event.setContent(contentString);
            event.setDateTime(eventTime.toDate());
            event.setCreateDateTime(DateTime.now().toDate());

            if (getInstance().isConnectWatch()) {
                BlueToothUtil.syncRemindEvent(0, event);
                databaseHelper.insertRemindEvent(event);

                setResult(RESULT_OK);
                finish();
            } else {
                showText(getString(R.string.add_remind_event_failure));
            }
        }
    }
}
